<?php


namespace App\Logic;

use App\Models\Objective_qModel;
use App\Models\QuestionModel;
use App\Models\Subjective_qModel;
use App\Utils\Singleton;
use App\Models\RandomModel;
use App\Models\ExamModel;
use \Illuminate\Http\Request;


class PaperLogic
{
    use Singleton;

    public function add(Request $request)
    {
        $answer_sheets=$request->input('answer_sheets');
         //var_dump($request->input());
        //var_dump($answer_sheets);
        $stu_id=$request->input('stu_id');
        foreach ($answer_sheets as $v){
            if ($v['question_type'] == '单选题'){
                $single_num=0;
                $single_score=0;
                $subject_id=objectToArray(QuestionModel::getInstance()->getOne('subject_id',
                    ['question_id'=>$v['sheet'][0]['question_id']]))['subject_id'];
                if (!empty($score=objectToArray(ExamModel::getInstance()->getOne('question_score',[
                    'subject_id'=>$subject_id,'type'=>$v['question_type']
                ]))['question_score'])){

                }elseif (!empty($score=objectToArray(RandomModel::getInstance()->getOne('question_score',[
                    'subject_id'=>$subject_id,'type'=>$v['question_type']
                ]))['question_score'])){

                }else{
                    break;
                }
                foreach ($v['sheet'] as $value){
                    //question_answer' => string 'A','question_id' => string '4'
                    $single_num=$single_num+1;

                    $answer=objectToArray(QuestionModel::getInstance()->getOne('answer',
                        ['question_id'=>$value['question_id']]))['answer'];
                    if ($answer ==$value['question_answer']){
                        $single_score=$single_score+$score;
                    }
                }
            }elseif ($v['question_type'] == '多选题'){
                $more_num=0;
                $more_score=0;
                $subject_id=objectToArray(QuestionModel::getInstance()->getOne('subject_id',
                    ['question_id'=>$v['sheet'][0]['question_id']]))['subject_id'];
                if (!empty($score=objectToArray(ExamModel::getInstance()->getOne('question_score',[
                    'subject_id'=>$subject_id,'type'=>$v['question_type']
                ]))['question_score'])){

                }elseif (!empty($score=objectToArray(RandomModel::getInstance()->getOne('question_score',[
                    'subject_id'=>$subject_id,'type'=>$v['question_type']
                ]))['question_score'])){

                }else{
                    break;
                }
                foreach ($v['sheet'] as $value){
                    $more_num=$more_num+1;
                    $answer=objectToArray(QuestionModel::getInstance()->getOne('answer',
                        ['question_id'=>$value['question_id']]))['answer'];
                    if ($answer ==$value['question_answer']){
                        $more_score=$more_score+$score;
                    }
                }
            }elseif ($v['question_type'] == '判断题'){
                $judge_num=0;
                $judge_score=0;
                $subject_id=objectToArray(QuestionModel::getInstance()->getOne('subject_id',
                    ['question_id'=>$v['sheet'][0]['question_id']]))['subject_id'];
                if (!empty($score=objectToArray(ExamModel::getInstance()->getOne('question_score',[
                    'subject_id'=>$subject_id,'type'=>$v['question_type']
                ]))['question_score'])){

                }elseif (!empty($score=objectToArray(RandomModel::getInstance()->getOne('question_score',[
                    'subject_id'=>$subject_id,'type'=>$v['question_type']
                ]))['question_score'])){

                }else{
                    break;
                }
                foreach ($v['sheet'] as $value){
                    $judge_num=$judge_num+1;
                    $answer=objectToArray(QuestionModel::getInstance()->getOne('answer',
                        ['question_id'=>$value['question_id']]))['answer'];
                    if ($answer ==$value['question_answer']){
                        $judge_score=$judge_score+$score;
                    }
                }
            }elseif ($v['question_type'] == '简答题'){
                $subject_id=objectToArray(QuestionModel::getInstance()->getOne('subject_id',
                    ['question_id'=>$v['sheet'][0]['question_id']]))['subject_id'];
                if (!empty($score=objectToArray(ExamModel::getInstance()->getOne('question_score',[
                    'subject_id'=>$subject_id,'type'=>$v['question_type']
                ]))['question_score'])){

                }elseif (!empty($score=objectToArray(RandomModel::getInstance()->getOne('question_score',[
                    'subject_id'=>$subject_id,'type'=>$v['question_type']
                ]))['question_score'])){

                }else{
                    break;
                }

                foreach ($v['sheet'] as $value){
                    $insertData=[];
                    $question_content=objectToArray(QuestionModel::getInstance()->getOne('question_content',
                        ['question_id'=>$value['question_id']]))['question_content'];

                    $subjective_Data=[
                        'stu_id'=>$stu_id,
                        'question_id'=>$value['question_id'],
                        'question_content'=>$question_content,
                        'subjective_score'=>$score,
                        'subjective_answer'=>$value['question_answer']
                    ];

                    Subjective_qModel::getInstance()->insertData($subjective_Data);
                }
            }
        }
        if (empty($single_num)){
            $single_num=0;
        }
        if (empty($single_score)){
            $single_score=0;
        }
        if (empty($more_num)){
            $more_num=0;
        }
        if (empty($more_score)){
            $more_score=0;
        }
        if (empty($judge_num)){
            $judge_num=0;
        }
        if (empty($judge_score)){
            $judge_score=0;
        }

        $objective_Data=[
            'single_score'=>$single_score,
            'single_num'=>$single_num,
            'more_score'=>$more_score,
            'more_num'=>$more_num,
            'judge_score'=>$judge_score,
            'judge_num'=>$judge_num,
            'stu_id'=>$stu_id
        ];


        Objective_qModel::getInstance()->insertData($objective_Data);

    }
}
